package com.netdevsdk.demo;

import com.netdevsdk.demo.alarm.Alarm;
import com.netdevsdk.lib.NetDEVSDKLib;
import com.sun.jna.Pointer;

import javax.swing.*;

/**
 * 报警监听器 - 封装摄像头登录、报警注册与监听功能
 */
public class AlarmMonitor {
    
    /**
     * 启动报警监听
     */
    public static void startMonitoring() {
        // 1. 登录摄像头（复用 AutoLogin 逻辑）
        AutoLogin.allLogin();
        
        if (NetDemo.lpUserID == null) {
            System.out.println("❌ 登录失败，无法启动报警监听");
            return;
        }
        
        // 2. 注册报警回调
        boolean bRet = NetDemo.netdevsdk.NETDEV_SetAlarmFGCallBack(NetDemo.lpUserID, NetDemo.pfAlarmMessFGCB, NetDemo.lpUserID);
        if (!bRet) {
            System.out.println("❌ 报警回调注册失败，错误码: " + NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }
        
        // 3. 订阅报警事件
        NetDEVSDKLib.NETDEV_LAPI_SUB_INFO_S stSubInfo = new NetDEVSDKLib.NETDEV_LAPI_SUB_INFO_S();
        NetDEVSDKLib.NETDEV_SUBSCRIBE_SUCC_INFO_S stSubSuccInfo = new NetDEVSDKLib.NETDEV_SUBSCRIBE_SUCC_INFO_S();
        stSubInfo.udwType = 1024; // 报警类型
        stSubInfo.udwLibIDNum = 0xffff; // 库ID
        
        bRet = NetDemo.netdevsdk.NETDEV_SubscibeLapiAlarm(NetDemo.lpUserID, stSubInfo, stSubSuccInfo);
        if (!bRet) {
            System.out.println("❌ 报警订阅失败，错误码: " + NetDemo.netdevsdk.NETDEV_GetLastError());
        } else {
            System.out.println("✅ 报警订阅成功，订阅ID: " + stSubSuccInfo.udwID);
            NetDemo.dwAccessControlFGMointerID = stSubSuccInfo.udwID; // 保存订阅ID
        }
        
        // 4. 监听报警信息（通过 pfAlarmMessFGCB 回调）
        System.out.println("🚀 报警监听已启动，等待报警事件...");
    }

    public static void main(String[] args) {
        startMonitoring();
        subscribeAlarm();
        // 持续运行监听
        while (true) {
            try {
                Thread.sleep(1000); // 避免CPU占用过高
            } catch (InterruptedException e) {
                System.out.println("❌ 监听线程被中断: " + e.getMessage());
                break;
            }
        }
    }

    public static void subscribeAlarm() {
        if(null == NetDemo.lpUserID)
        {
            JOptionPane.showMessageDialog(null, "Please Login device first. error code"+NetDemo.netdevsdk.NETDEV_GetLastError());
            return;
        }

        //IPC或nvr需单独订阅15,bit13(8192)，并注册V40回调和图片回调
            //注册告警回调
            boolean bRet = NetDemo.netdevsdk.NETDEV_SetAlarmCallBack_V40(NetDemo.lpUserID, NetDemo.cbAlarmMessCallBackV40, null);
            if(false == bRet) {
                System.out.printf("NETDEV_SetAlarmCallBack_V40 failed,error code"+ NetDemo.netdevsdk.NETDEV_GetLastError());
            }
            else{
                System.out.printf("NETDEV_SetAlarmCallBack_V40 success");
            }
            //注册图片回调
            bRet = NetDemo.netdevsdk.NETDEV_SetPicAlarmCallBack(NetDemo.lpUserID, NetDemo.cbPicMessCallBack, NetDemo.lpUserID);
            if(false == bRet) {
                System.out.printf("NETDEV_SetPicAlarmCallBack failed,error code"+ NetDemo.netdevsdk.NETDEV_GetLastError());
            }
            else{
                System.out.printf("NETDEV_SetPicAlarmCallBack success");
            }

            //订阅告警
            NetDEVSDKLib.NETDEV_LAPI_SUB_INFO_S stSubInfo = new NetDEVSDKLib.NETDEV_LAPI_SUB_INFO_S();
            stSubInfo.udwType = 15;
            NetDEVSDKLib.NETDEV_SUBSCRIBE_SUCC_INFO_S stSubSuccInfo = new NetDEVSDKLib.NETDEV_SUBSCRIBE_SUCC_INFO_S();
            bRet = NetDemo.netdevsdk.NETDEV_SubscibeLapiAlarm(NetDemo.lpUserID, stSubInfo, stSubSuccInfo);
            if(false == bRet){
                System.out.printf("NETDEV_SubscibeLapiAlarm failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
                //JOptionPane.showMessageDialog(null, "NETDEV_SubscibeLapiAlarm 15 failed");
                return;
            }
            else{
                NetDemo.udwAlarmSubID = stSubSuccInfo.udwID;
                System.out.printf("NETDEV_SubscibeLapiAlarm 15 succ\n");
                //JOptionPane.showMessageDialog(null, "NETDEV_SubscibeLapiAlarm 15 success");
            }
            //订阅图片
            stSubInfo.udwType = 8192;
            bRet = NetDemo.netdevsdk.NETDEV_SubscibeLapiAlarm(NetDemo.lpUserID, stSubInfo, stSubSuccInfo);
            if(false == bRet){
                System.out.printf("NETDEV_SubscibeLapiAlarm failed:%d\n", NetDemo.netdevsdk.NETDEV_GetLastError());
                //JOptionPane.showMessageDialog(null, "NETDEV_SubscibeLapiAlarm bit13 failed");
                return;
            }
            else{
                NetDemo.udwPicSubID = stSubSuccInfo.udwID;
                System.out.printf("NETDEV_SubscibeLapiAlarm bit13 succ\n");
                //JOptionPane.showMessageDialog(null, "NETDEV_SubscibeLapiAlarm bit13 success");
            }


        //vms在 NETDEV_SetAlarmCallBack_V30 内部订阅
        boolean bRet1 = NetDemo.netdevsdk.NETDEV_SetAlarmCallBack_V30(NetDemo.lpUserID, NetDemo.cbAlarmMessCallBack, null);
        if(bRet1) {
            JOptionPane.showMessageDialog(null, "Subscribe success");
        }else {
            JOptionPane.showMessageDialog(null, "NETDEV_SetAlarmCallBack_V30 failed,error code"+ NetDemo.netdevsdk.NETDEV_GetLastError());
        }
    }
    
    /**
     * 打印报警信息
     */
    public static void printAlarmMessage(String message) {
        System.out.println("🔔 报警信息: " + message);
    }
}